package com.ossjk.oa.system.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.util.Constant;
import com.ossjk.core.util.CryptoUtils;
import com.ossjk.oa.system.entity.Department;
import com.ossjk.oa.system.service.IDepartmentService;

/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author chair
 * @since 2019-01-14
 */
@RestController
@RequestMapping("/system/department")
public class DepartmentController extends BaseController {
	@Autowired
	private IDepartmentService iDepartmentService;

	@RequiresPermissions("00000005")
	@GetMapping("/list")
	public Object list(String name, Page page) {
		Wrapper wrapper = Condition.create().like("name", name).orderBy("level", true).orderBy("sort", true);
		page = iDepartmentService.selectPage(page, wrapper);
		return responseSuccess(page);
	}

	@RequiresPermissions("000000050000")
	@PostMapping("/insert")
	public Object insert(Department department) {
		
		
		Wrapper wrapper=Condition.create().eq("name", department.getName());
		if(!CommonUtil.isBlank(iDepartmentService.selectList(wrapper))) {
			return responseFail("名字已存在。");
		}
		
		
		if (CommonUtil.isEquals(department.getLevel(), 1)) {
			department.setPid("0");
		}
		if (iDepartmentService.insert(department)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("000000050005")
	@GetMapping("/toUpdate/{id}")
	public Object toUpdate(@PathVariable("id") String id) {
		return responseSuccess(iDepartmentService.selectById(id));
	}

	@RequiresPermissions("000000050005")
	@PutMapping("/update")
	public Object update(Department department) {
		Wrapper wrapper=Condition.create().eq("name", department.getName());
		if(!CommonUtil.isBlank(iDepartmentService.selectList(wrapper))) {
			return responseFail("名字已存在。");
		}
		if (iDepartmentService.updateById(department)) {
			return responseSuccess();
		} else {
			return responseFail();
		}

	}

	@RequiresPermissions("000000050010")
	@DeleteMapping("/delete/{id}")
	public Object delete(@PathVariable("id") String[] id) {
		if (iDepartmentService.deleteBatchIds(Arrays.asList(id))) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}
}
